(function (mui, $) {
var page  = window.page = {};
//1:未使用,2:已使用,3:已过期
var _Data = {'token':app.store('token'), 'page':0, 'nums':20, 'type':1, 'checkout':'0', 'products':''};

page.init = function (data) {
	call_on_pull_down();
};
page.receive = function () {
	var btnArray = ['取消', '确定'];
	mui.prompt('请输入优惠码', '', '提示!', btnArray, function(e) {
		var v = e.value.trim();
		if (e.index == 0) { return; }
		
		app.log('转赠优惠卷：', {'token':app.store('token'), 'data':{'code':v}});
		plus.nativeUI.showWaiting();
		$.ajax({
			'dataType' : 'json',
			'type'     : 'post',
			'url'      : app.url('App/Member/Volume/receive'),
			'data'     : {'token':app.store('token'), 'data':{'code':v}},
		})
		.fail(function (res) {
			// 失败回调
			plus.nativeUI.closeWaiting();
			app.log('获取转赠优惠卷失败：', res);
			app.toast('获取转赠优惠卷失败');
		})
		.done(function (res) {
			// 成功回调
			plus.nativeUI.closeWaiting();
			if (500 == res.code) { app.toast('你还未登录'); return; }
			if (300 == res.code) { app.toast(res.msg); return; }
			if (200 != res.code) { app.toast('请求失败'); return; }
			
			call_on_pull_down();
		})
		;
	})
};

function next (cb) {
	app.log('请求参数：', mui.extend({}, _Data, {'page':_Data.page + 1}, {data:{type:_Data.type, checkout:_Data.checkout, products:_Data.products}}));
	if (plus.webview.currentWebview().isVisible()) { plus.nativeUI.showWaiting(); }
	$.ajax({
		'dataType' : 'json',
		'type'     : 'post',
		'url'      : app.url('App/Member/Volume/lists'),
		'data'     : mui.extend({}, _Data, {'page':_Data.page + 1}, {data:{type:_Data.type, checkout:_Data.checkout, products:_Data.products}}),
	})
	.fail(function (res) {
		// 失败回调
		plus.nativeUI.closeWaiting();
		app.log('获取订单列表失败：', res);
		app.toast('获取订单列表失败');
		app.tryResetPullLoading();
	})
	.done(function (res) {
		// 成功回调
		plus.nativeUI.closeWaiting();
		app.log('订单数据：', res);
		
		if (500 == res.code) { app.toast('你还未登录'); app.tryResetPullLoading(); return; }
		if (300 == res.code) { app.toast(res.msg);     app.tryResetPullLoading(); return; }
		if (200 != res.code) { app.toast('请求失败!');  app.tryResetPullLoading(); return; }
		if (res.msg && plus.webview.currentWebview().isVisible()) { app.toast(res.msg); }
		
		res.type = _Data.type;
		if (res.data && res.data.length) { _Data.page++; }
		cb && cb(res);
	})
	;
}
// 下拉刷新
function call_on_pull_down () {
	var pullrefresh = mui('.container-pull-refresh').pullRefresh();
	_Data.page = 0;
	$(pullrefresh.element).find('.mui-scroll>.scroll-data').empty();
	
	next (function (res) {
		var dom = $(pullrefresh.element).find('.mui-scroll>.scroll-data');
		$(dom).append(app.tpl($(dom).attr('data-tpl'), res));
		pullrefresh.endPulldownToRefresh();
	});
}
// 上拉加载
function call_on_pull_up () {
	var pullrefresh = mui('.container-pull-refresh').pullRefresh();
	
	next (function (res) {
		var dom = $(pullrefresh.element).find('.mui-scroll>.scroll-data');
		var ele = app.tpl($(dom).attr('data-tpl'), res);
		if ($(ele).hasClass('no-data')) {
			if (0 == _Data.page) { $(dom).empty(); } else { $(ele).remove(); ele = null; }
		}
		$(dom).append(ele);
		pullrefresh.endPullupToRefresh();
	});
}
mui.init({
	pullRefresh   : {
		container : '.container-pull-refresh',
		down      : {callback : call_on_pull_down},
		up        : {callback : call_on_pull_up},
	}
});

mui.plusReady(function() {
	var view = plus.webview.currentWebview();
	if ('1' != (_Data.type = app.getQueryString('type'))) { view.hide(); }
	_Data.checkout = 'checkout.voucher.html' == view.parent().id ? '1' : '0';
	_Data.products = view.extras.products || '0';
	
	setTimeout(page.init, mui.os.ios ? 300 : 1000);
});

// 选择优惠卷
$('body').delegate('.groupon', 'tap', function () {
	var dom = this, view = plus.webview.currentWebview();
	//if ('checkout.voucher.html' == view.extras.id) {
	if ('1' != (_Data.type = app.getQueryString('type'))) {
		if ($(dom).hasClass('gray')) { app.toast('此优惠卷在本单不可使用'); return; }
		var data = {
			'vid'   : $(dom).attr('data-id'),
			'vname' : encodeURI($(dom).find('small').text() + ' ' + $(dom).find('.show-name').text()),
		}
		app.open(app.popHis(), data);
		// var code = 'page.onVoucherProvided('+JSON.stringify(data)+')';
		// plus.webview.getWebviewById('checkout.html').evalJS(code);
		
		// view.parent().close();
	}
});

})(mui, $)
